package com.learn.project2.service.wx.aftersale;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.*;
import com.learn.project2.mapper.MarketAftersaleMapper;
import com.learn.project2.mapper.MarketOrderGoodsMapper;
import com.learn.project2.mapper.MarketOrderMapper;
import com.learn.project2.model.bo.wx.aftersale.AftersaleListBO2;
import com.learn.project2.model.bo.wx.aftersale.AftersaleSubmitBO2;
import com.learn.project2.model.vo.wx.aftersale.AftersaleDetailVO2;
import com.learn.project2.model.vo.wx.aftersale.AftersalePicVO2;
import com.learn.project2.model.vo.wx.aftersale.AftersaleVO2;
import com.learn.project2.utils.SecretUtils;
import com.learn.project2.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * \* Author: Valley
 * \* Date: 2022/1/12
 * \* Time: 15:45
 * \* Description:
 */
@Service
public class AftersaleServiceImpl2 implements AftersaleService2 {

    @Autowired
    MarketAftersaleMapper aftersaleMapper;
    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;
    @Autowired
    MarketOrderMapper orderMapper;

    @Override
    public Integer submit(AftersaleSubmitBO2 aftersale) {
        MarketAftersale aftersale1 = new MarketAftersale();
        Subject subject = SecurityUtils.getSubject();
        MarketUser principal = (MarketUser) subject.getPrincipal();
        aftersale1.setUserId(principal.getId());
        aftersale1.setAddTime(StringUtils.dateToDate(new Date()));
        aftersale1.setDeleted(false);
        aftersale1.setStatus((short)1);
        aftersale1.setAftersaleSn(UUID.randomUUID().toString());
        aftersale1.setPictures(StringUtils.arrayToString(aftersale.getPictures()));
        aftersale1.setAmount(aftersale.getAmount());
        aftersale1.setOrderId(Integer.parseInt(aftersale.getOrderId()));
        aftersale1.setReason(aftersale.getReason());
        aftersale1.setType(aftersale.getType());
        aftersale1.setComment(aftersale.getTypeDesc());
        int insert = aftersaleMapper.insert(aftersale1);
        return insert;
    }

    @Override
    public AftersaleVO2 aftersaleList(AftersaleListBO2 aftersaleListBO2) {
        PageHelper.startPage(aftersaleListBO2.getPage(),aftersaleListBO2.getLimit());
        MarketAftersaleExample aftersaleExample = new MarketAftersaleExample();
        aftersaleExample.createCriteria().andTypeEqualTo((short) aftersaleListBO2.getStatus());
        List<MarketAftersale> aftersales = aftersaleMapper.selectByExample(aftersaleExample);
        PageInfo<MarketAftersale> pageInfo = new PageInfo<>(aftersales);
        AftersaleVO2 aftersaleVO2 = new AftersaleVO2();
        aftersaleVO2.setLimit(aftersaleListBO2.getLimit());
        aftersaleVO2.setPage(aftersaleListBO2.getPage());
        aftersaleVO2.setPages(pageInfo.getPages());
        aftersaleVO2.setTotal(pageInfo.getTotal());

        List<AftersaleVO2.ListBean> listBean = new ArrayList<>();
        for (MarketAftersale aftersale : aftersales) {
            AftersaleVO2.ListBean bean = new AftersaleVO2.ListBean();
            bean.setAftersale(aftersale);
            MarketOrderGoodsExample example = new MarketOrderGoodsExample();
            example.createCriteria().andOrderIdEqualTo(aftersale.getOrderId());
            List<MarketOrderGoods> orderGoods = orderGoodsMapper.selectByExample(example);
            bean.setGoodsList(orderGoods);
            listBean.add(bean);
        }
        aftersaleVO2.setList(listBean);
        return aftersaleVO2;
    }

    @Override
    public AftersaleDetailVO2 aftersaleDetail(Integer orderId) {
        MarketOrder order = orderMapper.selectByPrimaryKey(orderId);
        AftersaleDetailVO2 detailVO2 = new AftersaleDetailVO2();
        order.setEndTime(StringUtils.dateToDate(new Date()));
        detailVO2.setOrder(order);
        MarketOrderGoodsExample goodsExample = new MarketOrderGoodsExample();
        goodsExample.createCriteria().andOrderIdEqualTo(orderId);
        List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(goodsExample);
        detailVO2.setOrderGoods(marketOrderGoods);
        MarketAftersaleExample aftersaleExample = new MarketAftersaleExample();
        aftersaleExample.createCriteria().andOrderIdEqualTo(orderId);
        List<MarketAftersale> aftersales = aftersaleMapper.selectByExample(aftersaleExample);
        MarketAftersale aftersale = aftersales.get(0);
        AftersalePicVO2 picVO2 = new AftersalePicVO2();
        picVO2.setId(aftersale.getId());
        picVO2.setUserId(aftersale.getUserId());
        picVO2.setDeleted(false);
        picVO2.setPictures(StringUtils.stringToStringArray(aftersale.getPictures()));
        picVO2.setAftersaleSn(aftersale.getAftersaleSn());
        picVO2.setAmount(aftersale.getAmount());
        picVO2.setReason(aftersale.getReason());
        picVO2.setStatus(aftersale.getStatus());
        picVO2.setType(aftersale.getType());
        picVO2.setUpdateTime(aftersale.getUpdateTime());
        picVO2.setAddTime(aftersale.getAddTime());
        picVO2.setComment(aftersale.getComment());
        picVO2.setHandleTime(aftersale.getHandleTime());
        detailVO2.setAftersale(picVO2);
        return detailVO2;
    }
}
